<?php
class PagesModel extends Model {
	//复制站点模块
	public function copy($siteid, $tositeid, $page_id = 0){
		$pageMap['site_id'] = $siteid;
		if($page_id > 0){
			$pageMap['id'] = $page_id;
		}
		if(is_array($page_id)){
			$pageMap['id'] = array('in',$page_id);
		}
		$ced = array();
		$ced[] = 0;
		$PagesList = $this -> where($pageMap) -> select();
		$db = D('Modules');
		foreach($PagesList as $k => $v){
			
			
			
			
			$v['site_id'] = $tositeid;
			$v['rid'] = $v['id'];
			$fpid = $v['id'];
			$pagesex = $this -> where(array('site_id' => $tositeid, 'rid' => $v['id'])) -> find();
			if($pagesex){
				$id = $pagesex['id'];
				continue;
			}
			//复制过了，不再复制
			unset($v['id']);
			
			$ced[] = $id = $this -> data($v) -> add();
			echo '<script>window.parent.progress("写入一个页面");</script>';
			ob_flush();
            flush();
			
			$db -> copy($siteid,$tositeid,array('site_id' => $siteid, 'pid' => 0, 'pages_id' => $fpid), 0, $id);
		}
		
		$this -> where(array('site_id' => $tositeid, 'id' => array('in', $ced))) -> save(array('rid' => 0));
		
		
		return $id;
	}
	
	
	//复制引用页面
	public function copyimport($siteid, $tositeid, $page_id = 0){
		
		if($siteid == $tositeid){return $page_id;}
		$pageMap['site_id'] = $siteid;
		if($page_id > 0){
			$pageMap['id'] = $page_id;
		}
		if(is_array($page_id)){
			$pageMap['id'] = array('in',$page_id);
		}
		$PagesList = $this -> where($pageMap) -> select();
		$db = D('Modules');
		$id = 0;
		if(!$PagesList){
			return 0;
		}
		foreach($PagesList as $k => $v){
			$v['site_id'] = $tositeid;
			$v['rid'] = $v['id'];
			$fpid = $v['id'];
			$pagesex = $this -> where(array('site_id' => $tositeid, 'rid' => $v['id'])) -> find();
			if($pagesex){
				$id = $pagesex['id'];
				continue;
			}
			//复制过了，不再复制
			unset($v['id']); 
			
			$id = $this -> data($v) -> add();
			
			
			$db -> copy($siteid,$tositeid,array('site_id' => $siteid, 'pid' => 0, 'pages_id' => $fpid), 0, $id);
		}
		
		return $id;
	}
	
	public function getListToArray($map,$view = ''){
		$mdb = D('Modules');
		$re=$mdb->where($map)->order('sort,id')->select();
		if($re!==false){
			unset($map['id']);
			unset($map['pages_id']);
			foreach($re as $k=>$v){
				$v['style'] = deleteScript($v['style']);
			    $v['content'] = deleteScript($v['content']);
				$arr = explode('.',$v['dataname']);
				if($view != '' && trim($arr[0]) !=''){
					if(is_array($view)){
					    $re[$k]['dataname'] = parseTemplateData($v['dataname'], $view[$arr[0]]);
					}else{
						$re[$k]['dataname'] = parseTemplateData($v['dataname'], $view -> get($arr[0]));
					}
				}
				$diydata = unserialize($mdb -> where(array('id' => $v['id'])) -> getField('diydata'));
				foreach($diydata as $key => $value){
					$re[$k]['diysource'] = str_replace('{'.$key.'}', replacebr($value), $re[$k]['diysource']);
					//$re[$k]['diysource'] = $view -> fetch('',$re[$k]['diysource']);
					if(strpos($key, '_link') > 0){
						if($value != ''){
							$re[$k]['diysource'] = str_replace('<'.$key.'>', '<a href="'.replacelink($value).'">', $re[$k]['diysource']);
							$re[$k]['diysource'] = str_replace('</'.$key.'>', '</a>', $re[$k]['diysource']);
						}else{
							$re[$k]['diysource'] = str_replace('<'.$key.'>', '', $re[$k]['diysource']);
							$re[$k]['diysource'] = str_replace('</'.$key.'>', '', $re[$k]['diysource']);
						}
					}
				}
				if($re[$k]['diysource'] != ''){
					$re[$k]['diysource'] .= '<div style="display:none;"></div>';
					$re[$k]['content'] = $view -> fetch('',$re[$k]['diysource']);
				}
				if($v['import'] > 0){
					$re[$k]['content'] = $mdb->getListToString($v['import'], $view, false, true);
					//dump($mdb->getListToString($v['import'], $view, false, true));
				}
				 
				
				
				//万能表单
				if($v['type'] == 16){
					$re[$k]['content'] = D('Form') -> where(array('id' => $v['form_id'])) ->createForm($view);
				}
				
				$map['pid'] = $v['id'];
				$re[$k]['sub']=$this->getListToArray($map, $view);
			}
			return $re;
		}
	}
	
	
	
	//返回全代码内容
	public function getListToStyle($pages_id, $view){
		$string = '';
		$modulesList = $this->getListToArray(array('pages_id' => $pages_id, 'pid' => 0), $view);
		foreach($modulesList as $k => $v){
			$view->assign('v',$v);
			$string.= $view->fetch(TMPL_PATH.'Home/TemplateDefault/Css/Modules-'.$v['filename'].C('TMPL_TEMPLATE_SUFFIX'));
		}
		return '<style>'.$string.'</style>';
	}
	
	//返回全代码内容 
	public function getListToString($pages_id, $view){
		$string = array();
		$modulesList = $this->getListToArray(array('pages_id' => $pages_id, 'pid' => 0), $view);
		foreach($modulesList as $k => $v){
			$view->assign('v',$v);
			$string['modules_'.$k] = $view->fetch(TMPL_PATH.'Home/TemplateDefault/Modules-'.$v['filename'].C('TMPL_TEMPLATE_SUFFIX'));
		} 
		$view->assign($string);
		//查找到页面
		 
		$page = $this-> where(array('id' => $pages_id)) -> find();
		//渲染
		if($page['full_source'] == ''){
			return '页面已经损坏！';
		}
		return $view -> fetch('',$page['full_source']);
	}
}